//package com.zmn.plat.task.job.contract;
//
//import com.github.ltsopensource.core.domain.Action;
//import com.github.ltsopensource.core.domain.Job;
//import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
//import com.github.ltsopensource.spring.tasktracker.LTS;
//import com.github.ltsopensource.tasktracker.Result;
//import com.zmn.common.constant.GlobalConsts;
//import com.zmn.common.dto.ResultDTO;
//import com.zmn.common.utils.date.DateUtil;
//import com.zmn.manager.lts.interfaces.task.TaskManager;
//import com.zmn.plat.common.dictionary.ContractStatusEnum;
//import com.zmn.plat.dubbo.dto.msg.MsgDIO;
//import com.zmn.plat.manager.utils.TaskConsts;
//import com.zmn.plat.model.entity.contract.record.ContractRecord;
//import com.zmn.plat.model.entity.contract.record.ContractRecordQuery;
//import com.zmn.plat.model.entity.msg.conf.MsgConf;
//import com.zmn.plat.services.interfaces.contract.record.ContractRecordService;
//import com.zmn.plat.services.interfaces.msg.conf.MsgConfService;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import javax.annotation.PostConstruct;
//import java.util.*;
//
//import static com.zmn.plat.common.constant.ContractConsts.CONTRACT_FIRST_ALERT_MSG_ID;
//import static com.zmn.plat.common.constant.ContractConsts.CONTRACT_OVERDUE_ALERT_MSG_ID;
//
///**
// * 合同过期预警JOB：
// *
// * @author linfeng
// * @since 2020/05/25 15:28:09
// */
//@LTS
//public class ContractOverdueAlertJob extends ContractAlertBaseJob {
//
//    private Logger logger = LoggerFactory.getLogger(ContractOverdueAlertJob.class);
//
//    @Autowired
//    private ContractRecordService contractRecordService;
//
//    @Autowired
//    private TaskManager taskManager;
//
//    @Autowired
//    private MsgConfService msgConfService;
//
//    /**
//     * Init.
//     */
//    @PostConstruct
//    void init() {
//        Job job = new Job();
//        job.setTaskId(TaskConsts.TASK_ID_CONTRACT_OVERDUE_ALERT);
//        job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_CONTRACT_OVERDUE_ALERT);
//        // 每日1点执行
//        job.setCronExpression("0 0 1 * * ?");
//        job.setReplaceOnExist(true);
//        ResultDTO result = taskManager.submitTask(job);
//        if (!result.getStatusBool()) {
//            logger.error("#contract overdue alert# job submit fail...");
//        }
//    }
//
//    /**
//     * 合同过期预警 根据配置的预警时间和预警人  发送站内信给预警接收人
//     *
//     * @param job the job
//     * @return result
//     */
//    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_CONTRACT_OVERDUE_ALERT)
//    public Result runContractOverdueAlertJob(Job job) {
//
//        logger.debug("#job#contract overdue alert#start running...");
//
//        MsgConf msgConf = msgConfService.findByKey(CONTRACT_OVERDUE_ALERT_MSG_ID);
//        if (msgConf == null) {
//            return new Result(Action.EXECUTE_FAILED, String.format("消息ID为%s的配置未找到", CONTRACT_OVERDUE_ALERT_MSG_ID));
//        }
//
//        if (Objects.equals(GlobalConsts.NO, msgConf.getStatus())) {
//            return new Result(Action.EXECUTE_FAILED, String.format("消息ID为%s的配置状态未开启", CONTRACT_OVERDUE_ALERT_MSG_ID));
//        }
//
//        ContractRecordQuery contractRecordQuery = new ContractRecordQuery();
//        contractRecordQuery.setStatus(ContractStatusEnum.EXECUTION.getCode());
//        contractRecordQuery.setPerformance(GlobalConsts.NO);
//        contractRecordQuery.setOverdueAlert(GlobalConsts.YES);
//        contractRecordQuery.setOverdueAlertStatus(GlobalConsts.NO);
//
//        List<ContractRecord> records = contractRecordService.listByQuery(contractRecordQuery);
//        Integer count = 0;
//        if (records != null && records.size() > 0) {
//            for (ContractRecord contractRecord : records) {
//                Date contractEndDate = contractRecord.getContractEndDay();
//                Date now = DateUtil.getNow();
//
//                //得到相差的天数 betweenDate
//                long diff = now.getTime() - contractEndDate.getTime();
//                if (diff > 0) {
//                    MsgDIO msgDIO = new MsgDIO();
//                    msgDIO.setMsgId(msgConf.getMsgId());
//                    String params = contractRecord.getName();
//                    msgDIO.setParams(params);
//                    List<Integer> overdueAlertStaffIds = getOverdueAlertReceiverStaffIds(contractRecord);
//                    if (overdueAlertStaffIds != null && overdueAlertStaffIds.size() > 0) {
//                        msgDIO.setStaffIds(overdueAlertStaffIds);
//                    }
//                    boolean sendAllMessageSucess = sendAllMsg(msgDIO);
//                    if (sendAllMessageSucess) {
//                        contractRecord.setOverdueAlertStatus(GlobalConsts.YES);
//                        contractRecord.setUpdateTime(DateUtil.getNow());
//                        contractRecord.setUpdater("SYSTEM");
//                        contractRecordService.updateByKey(contractRecord);
//                    }
//                }
//
//            }
//        }
//
//        logger.debug("#job#contract_overdue_alert#end running... update count :" + count);
//        return new Result(Action.EXECUTE_SUCCESS, "合同过期预警任务完成! update count:" + count);
//    }
//    }
